
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The impact of divorce laws on the equilibrium in the marriage market.
% Reynoso
% April 2024
%
% This file inputs estimates and produces moments and utilities under the
% unilateral divorce regime.
%  
% Data: PSID 1968-1992
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%------------------------ Replication Path -------------------------------%

clc
clear all

%--- Indicate location of Replication folder:
%replication_location = 'C:\update_with_your_path';

%------------------------ Preliminaries ----------------------------------%

%--- Path for estimates
estimates_dir = [replication_location,'\Outputs\MCD\'];

%--- Path for functions
addpath(genpath([replication_location,'\Impact_UD\Inputs']));
addpath(genpath([replication_location,'\Internal_estimation\Inputs']));

%--- Path to store
output_dir = [replication_location,'\Outputs'];

%------------------------ UD moments -------------------------------------%

%---Full model
load([estimates_dir, 'outputs_mcd']); 
params=X;
load([output_dir, '\UD\equil_ud']); 
model_moments_bymkt_ud = zeros(168/4, 4); 
U_ud_bymkt = zeros(n_g,4,4); 
U_ud_tot_bymkt = zeros(n_g,2,4); 
pareto_weight_evol=ones(n_g,T,4);

parfor mkt=1:4
    [model_moments_bymkt_ud(:,mkt), pareto_weight_evol(:,:,mkt), U_ud_bymkt(:,:,mkt), U_ud_tot_bymkt(:,:,mkt)] =  moments_ud(n_g, T, lt, dt, H_max, disc, stoch_earn_f, stoch_earn_m, m_eta_f, m_eta_m, n_eta_f, n_eta_m, home_prod, match_qual, m_th, n_th, gamma, L_max, params, lambda_ud(:,mkt), data_f(:,mkt), data_m(:,mkt));
end

save(fullfile(output_dir,'\UD\U_ud'),'U_ud_bymkt','U_ud_tot_bymkt'); 
save(fullfile(output_dir,'\UD\moments_ud'),'model_moments_bymkt_ud');
save(fullfile(output_dir,'\UD\pweight_evol_ud'),'pareto_weight_evol');

cd([replication_location,'\Outputs\UD']);
run("Presentation_udeffects.m")
clearvars -except replication_location estimates_dir output_dir

%--- Divorce settlement
load([estimates_dir, 'outputs_mcd']); 
params=X;
load([output_dir, '\Divorce_settlement\equil_ud_DivorceSettlement']); 
model_moments_bymkt_ud = zeros(168/4, 4); 
U_ud_bymkt = zeros(n_g,4,4); 
U_ud_tot_bymkt = zeros(n_g,2,4); 
pareto_weight_evol=ones(n_g,T,4);

parfor mkt=1:4
    [model_moments_bymkt_ud(:,mkt), pareto_weight_evol(:,:,mkt), U_ud_bymkt(:,:,mkt), U_ud_tot_bymkt(:,:,mkt)] =  moments_ud_DivorceSettlement(n_g, T, lt, dt, H_max, disc, stoch_earn_f, stoch_earn_m, m_eta_f, m_eta_m, n_eta_f, n_eta_m, home_prod, match_qual, m_th, n_th, gamma, L_max, params, lambda_ud(:,mkt), data_f(:,mkt), data_m(:,mkt));
end

save(fullfile(output_dir,'\Divorce_settlement\U_ud_DivorceSettlement'),'U_ud_bymkt','U_ud_tot_bymkt'); 
save(fullfile(output_dir,'\Divorce_settlement\moments_ud_DivorceSettlement'),'model_moments_bymkt_ud');
save(fullfile(output_dir,'\Divorce_settlement\pweight_evol_ud_DivorceSettlement'),'pareto_weight_evol');

cd ([replication_location,'\Outputs\Divorce_settlement']);
run("Presentation_udeffects_DivorceSettlement.m")
clearvars -except replication_location estimates_dir output_dir

%--- Commitment
load([estimates_dir, 'outputs_mcd']); 
params=X;
load([output_dir, '\Commitment\equil_ud_Commitment']); 
model_moments_bymkt_ud = zeros(168/4, 4);
U_ud_bymkt = zeros(n_g,4,4); 
U_ud_tot_bymkt = zeros(n_g,2,4); 
pareto_weight_evol=ones(n_g,T,4);

parfor mkt=1:4
    [model_moments_bymkt_ud(:,mkt), pareto_weight_evol(:,:,mkt), U_ud_bymkt(:,:,mkt), U_ud_tot_bymkt(:,:,mkt)] =  moments_ud_Commitment(n_g, T, lt, dt, H_max, disc, stoch_earn_f, stoch_earn_m, m_eta_f, m_eta_m, n_eta_f, n_eta_m, home_prod, match_qual, m_th, n_th, gamma, L_max, params, lambda_ud(:,mkt), data_f(:,mkt), data_m(:,mkt));
end

save(fullfile(output_dir,'\Commitment\U_ud_Commitment'),'U_ud_bymkt','U_ud_tot_bymkt'); 
save(fullfile(output_dir,'\Commitment\moments_ud_Commitment'),'model_moments_bymkt_ud');
save(fullfile(output_dir,'\Commitment\pweight_evol_ud_Commitment'),'pareto_weight_evol');

cd ([replication_location,'\Outputs\Commitment']);
run("Presentation_udeffects_Commitment.m")







